#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=510;
int n,m,head[MAXN<<1],cnt,p[MAXN],mp1[MAXN][MAXN],mp2[MAXN][MAXN];
struct node
{
	int u,v,d;
}a[MAXN];
struct edge
{
	int v,next,val;
}e[MAXN*MAXN*2];
void addedge(int x,int y,int z)
{
	e[cnt]=(edge){y,head[x],z};
	head[x]=cnt++;
	swap(x,y);
	e[cnt]=(edge){y,head[x],0};
	head[x]=cnt++;
	return;
}
struct MF
{
	int idx[MAXN<<1],S,T,cur[MAXN<<1];
	void bfs()
	{
		memset(idx,0,sizeof(idx));
		queue<int>q;
		q.push(S);
		idx[S]=1;
		while(!q.empty())
		{
			int u=q.front();q.pop();
			for(int i=head[u];~i;i=e[i].next)
			{
				int v=e[i].v;
				if(!e[i].val||idx[v])continue;
				idx[v]=idx[u]+1;
				q.push(v);
			}
		}
		return;
	}
	int dfs(int u,int _min)
	{
		if(u==T)return _min;
		int ret=0,k;
		for(int i=cur[u];~i;i=e[i].next)
		{
			int v=e[i].v;
			cur[u]=i;
			if(!e[i].val||idx[v]!=idx[u]+1)continue;
			k=dfs(v,min(_min-ret,e[i].val));
			e[i].val-=k;
			e[i^1].val+=k;
			ret+=k;
			if(ret==_min)break;
		}
		return ret;
	}
	int dinic()
	{
		int ret=0;
		while(true)
		{
			bfs();
			if(!idx[T])break;
			memcpy(cur,head,sizeof(head));
			ret+=dfs(S,INF);
		}
		return ret;
	}
}T1;
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	n=read();m=read();
	for(int i=1;i<=n;++i)p[i]=read();
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			mp1[i][j]=mp2[i][j]=read();
	for(int k=1;k<=n;++k)
		for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				mp2[i][j]=min(mp2[i][j],mp2[i][k]+p[k]+mp2[k][j]);
	for(int i=1;i<=m;++i)a[i].u=read(),a[i].v=read(),a[i].d=read();
	memset(head,-1,sizeof(head));
	int S=0,T=m*2+1;
	for(int i=1;i<=m;++i)
	{
		addedge(S,i*2-1,1);addedge(i*2,T,1);
		for(int j=1;j<=m;++j)
		{
			if(i==j)continue;
			if(a[i].v==a[j].u&&a[i].d+mp1[a[i].u][a[i].v]+p[a[i].v]<=a[j].d)addedge(i*2-1,j*2,INF);
			else if(a[i].d+mp1[a[i].u][a[i].v]+p[a[i].v]+mp2[a[i].v][a[j].u]+p[a[j].u]<=a[j].d)addedge(i*2-1,j*2,INF);
		}
	}
	T1.S=S;T1.T=T;
	printf("%d\n",m-T1.dinic());
	return 0;
}
